package com.damagez.selfrefresh.annotation;

import com.damagez.selfrefresh.redis.RefreshRedisCacheKeyGenerator;
import com.damagez.selfrefresh.redis.RefreshRedisCacheManager;
import com.damagez.selfrefresh.util.StringToDurationConverter;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.*;

/**
 * @author damage
 * @create 2020/5/9 16:29
 * @description
 * 缓存名字的分割符为#
 * 例如：result#3m#30s#5，3m表示缓存的过期时间为3分钟，30s表示缓存距离失效时间前30s内，5表示至少调用5次缓存
 * 在缓存距离失效前30s内，如果缓存调用次数超过5次，则再次调用会触发更新缓存的操作
 *
 * 定义缓存过期时间和缓存距离失效时间需要使用{@link StringToDurationConverter}所指定的语法。
 * 与spring boot的yml文件中指定时间的用法相同
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Cacheable
public @interface RefreshCacheable {

    @AliasFor(value = "cacheNames",annotation = Cacheable.class)
    String[] cacheNames() default {};

    @AliasFor(value = "key",annotation = Cacheable.class)
    String key() default "";

    @AliasFor(value = "keyGenerator",annotation = Cacheable.class)
    String keyGenerator() default RefreshRedisCacheKeyGenerator.BEAN_NAME;

    @AliasFor(value = "cacheManager",annotation = Cacheable.class)
    String cacheManager() default RefreshRedisCacheManager.BEAN_NAME;

    @AliasFor(value = "cacheResolver",annotation = Cacheable.class)
    String cacheResolver() default "";

    @AliasFor(value = "condition",annotation = Cacheable.class)
    String condition() default "";

    @AliasFor(value = "unless",annotation = Cacheable.class)
    String unless() default "";

    @AliasFor(value = "sync",annotation = Cacheable.class)
    boolean sync() default false;

}
